package vip.specimen.system.controller.authorization;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.VerificationCodeUtil;
import vip.specimen.system.result.JsonResult;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * *@Author: 余浪人
 * *@Date：2021/2/4 17:25
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "验证码")
@RestController
@RequestMapping(value = "/verificationCode")
public class VerifCodeController {
    @Value("${system.verification.key}")
    String key;
    @Value("${system.verification.expire}")
    long expire;

    /**
     * 获取验证码
     *
     * @param request 请求
     * @return 验证码
     */
    @GetMapping(value = "getCode")
    @ApiOperation(value = "IP验证码", notes = "基于请求IP获取验证码")
    public JsonResult ip(HttpServletRequest request) {
        return JsonResult.success(new HashMap<String, String>() {
            {
                put("code", new VerificationCodeUtil(key, CommonUtil.getIp(request), expire).getCode());
            }
        });
    }

//    @GetMapping(value = "phone/{phone}")
//    public JsonResult phone(@PathVariable String phone) {
//        if (CommonUtil.phone(phone)) {
//            new VerificationCodeUtil(key, phone, expire).getCode();//  todo 发送短信(优先发送短信，成功后写入redis)
//            return JsonResult.success("手机验证码发送成功");
//        }
//        return JsonResult.failed(ResultCode.ERROR_PARAMETER);
//    }

//    @GetMapping(value = "email/{email}")
//    public JsonResult email(@PathVariable String email) {
//        if (CommonUtil.email(email)) {
//            new VerificationCodeUtil(key, email, expire).getCode();//  todo 发送邮件(优先发送邮件，成功后写入redis)
//            return JsonResult.success();
//        }
//        return JsonResult.failed(ResultCode.ERROR_PARAMETER);
//    }

//    @GetMapping(value = "account/{account}")
//    public JsonResult account(@PathVariable String account) {
//        if (null == account && account.length()<1){
//            return JsonResult.failed(ResultCode.ERROR_PARAMETER);
//        }
//        return JsonResult.success(new HashMap<String, String>() {
//            {
//                put("code", new VerificationCodeUtil(key, account, expire).getCode());
//            }
//        }, "账号验证码获取成功");
//    }
}
